/* -----------------------------------------------------------------------
 * Unit Test 3 = Testing gainCard
 * Using provided template testUpdateCoins.c
 * Testing using one player and one supply card
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <stdlib.h>
#include <time.h>

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

void testGainCard() {
    int flag;
    int seed = 1000;
    int numPlayer = 2;
    int supply = 10;
    int deckC = 5;
    int handC = 5;
    int discardC = 0;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

    memset(&G, 23, sizeof(struct gameState));   // clear the game state
    initializeGame(numPlayer, k, seed, &G); // initialize a new game
    //Pre-set all variables
    G.supplyCount[0] = supply;
    G.deckCount[0] = 5;
    G.handCount[0] = 5;
    G.discardCount[0] = 0;


    printf ("TESTING gainCard():\n");
    while (G.supplyCount[0] != 0)
    {
#if (NOISY_TEST == 1)
        printf("Test player %d with %d, %d, %d cards in their deck, hand, and discard piles respectively.\n", 0, deckC, handC, discardC);
        printf("Expected to have %d deck cards, %d hand cards, and %d discard cards.", G.deckCount[0], G.handCount[0], G.discardCount[0]);
#endif
        
        flag = rand()%3;
        if (flag == 0)
            discardC++;
        else if (flag == 1)
            deckC++;
        else //flag == 2
            handC++;
        supply--;
        gainCard(0, &G, flag, 0);

#if (NOISY_TEST == 1)
        printf("G.supplyCount = %d, expected = %d\n", G.supplyCount[0], supply);
        printf("G.deckCount, G.handCount, G.discardCount = %d, %d, %d expected = %d, %d, %d", G.deckCount[0], G.handCount[0], G.discardCount[0],
            deckC, handC, discardC);
#endif
        assert(G.supplyCount[0] == supply); // check if the supply is correct
        assert(G.deckCount[0] == deckC);
        assert(G.handCount[0] == handC);
        assert(G.discardCount[0] == discardC);

    }

    //Once supply goes to zero, supplyCount should return -1
#if (NOISY_TEST == 1)
    printf("When G.supplyCount = %d, expected output -1", G.supplyCount[0]);
#endif    
    assert(gainCard(0, &G, flag, 0) == -1);

    printf("All tests passed!\n");
}

int main() {
    srand(time(NULL));
    testGainCard();
    return 0;
}